package main.回溯;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author tmh
 * @date 2024/7/23 17:33
 * @description
 */
public class T131分割回文串 {
    List<List<String>> result = new ArrayList<>();
    LinkedList<String> path = new LinkedList<>();

    public List<List<String>> partition(String s) {
        backTracking(s,0);
        return result;
    }

    private void backTracking(String s, int startIndex) {
        if (startIndex >= s.length()) {
            result.add(new ArrayList<>(path));
        }
        for (int i = startIndex; i < s.length(); i++) {
            if (check(s, startIndex, i)){
                String str = s.substring(startIndex,i+1);
                path.add(str);
            }else {
                continue;
            }
            backTracking(s,i+1);
            path.removeLast();
        }
    }

    //判断是否是回文
    private boolean check(String s, int start, int end) {
        for (int i = start, j = end; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}
